package com.telecom.cms.service;

import java.io.File;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

import com.ncr.teradata.bireport.InitParam;
import com.telecom.cms.dao.Dao;
import com.telecom.cms.dao.DaoImp;
import com.telecom.cms.dao.CampCustInfoDAO;
import com.telecom.cms.dao.CampCustAdvInfoDAO;
import com.telecom.cms.dao.PreviewRsltDAO;
import com.telecom.cms.dto.PreviewSet;

import com.telecom.cms.util.Constant;

public class PreviewMgr {
   
	HashMap campsqlmap=new  HashMap();
		
	public List readpreview(String previewid){
		List result=new ArrayList();
		PreviewSet tmap=null;
		String reportConfFile = this.getClass().getResource("CampCustInfoMgr.class").getPath();
		reportConfFile =  reportConfFile.substring(0,reportConfFile.indexOf("WEB-INF")) + "WEB-INF/classes/"+InitParam.PREVIEW_SETTING_FILE;

				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				factory.setValidating(false);
				DocumentBuilder builder;
				try {
					builder = factory.newDocumentBuilder();
				
					Document doc = builder.parse(new File(reportConfFile));
	
					Node book=doc.getElementsByTagName(previewid).item(0);
					if(book!=null){
						System.out.println(book.getNodeName()+":"+book.getNodeValue());
						 for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()){
							 
							//System.out.println(node.getNodeName()+"b"+node.getNodeType());
							if(node.getNodeType()==Node.ELEMENT_NODE){
								//tmap=new PreviewSet();
								for(Node node2=node.getFirstChild();node2!=null;node2=node2.getNextSibling()){
									if(node2.getNodeType()==Node.ELEMENT_NODE)
									{
										String nodevalue=node2.getFirstChild()!=null?node2.getFirstChild().getNodeValue().trim():"";
										if(node2.getNodeName().trim().equals("exec"))
											;//result.add(nodevalue);
										else if(node2.getNodeName().trim().equals("step_id"))
										{
											if(tmap!=null) result.add(tmap);	
											tmap=new PreviewSet();
											tmap.setStep_id(nodevalue);
										}
										else if(node2.getNodeName().trim().equals("step_name"))
											tmap.setStep_name(nodevalue);
										else if(node2.getNodeName().trim().equals("step_comment"))
											tmap.setStep_comment(nodevalue);
										else if(node2.getNodeName().trim().equals("step_cache"))
											tmap.setStep_cache(nodevalue);
										else if(node2.getNodeName().trim().equals("step_type"))
											tmap.setStep_type(nodevalue);
										else if(node2.getNodeName().trim().equals("step_data"))
											tmap.setStep_data(nodevalue);
										else if(node2.getNodeName().trim().equals("step_default"))
											tmap.setStep_default(nodevalue);
										else if(node2.getNodeName().trim().equals("step_next"))
											tmap.setStep_next(nodevalue);
										else if(node2.getNodeName().trim().equals("step_append"))
											tmap.setStep_append(nodevalue);
									}
								}
							}
						 }
						 if(tmap!=null) result.add(tmap);	
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return result;

	}
	
	public List findall(String resulttable,String tableid,String ordertfield,String ordertype){
		
		List result=(new PreviewRsltDAO(resulttable)).findAll(tableid,ordertfield,ordertype);
		return result;
	}
	
	public List findalltableid(String resulttable){
		
		List result=(new PreviewRsltDAO(resulttable)).findAlltableid();
		return result;
	}
	
	public String readpreviewpara(String previewid,String para){
		String result="";
		String reportConfFile = this.getClass().getResource("CampCustInfoMgr.class").getPath();
		reportConfFile =  reportConfFile.substring(0,reportConfFile.indexOf("WEB-INF")) + "WEB-INF/classes/"+InitParam.PREVIEW_SETTING_FILE;

				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				factory.setValidating(false);
				DocumentBuilder builder;
				try {
					builder = factory.newDocumentBuilder();
				
					Document doc = builder.parse(new File(reportConfFile));
	
					Node book=doc.getElementsByTagName(previewid).item(0);
					if(book!=null){
						System.out.println(book.getNodeName()+":"+book.getNodeValue());
						 for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()){
							 
							//System.out.println(node.getNodeName()+"b"+node.getNodeType());
							if(node.getNodeType()==Node.ELEMENT_NODE){
								//tmap=new PreviewSet();
								for(Node node2=node.getFirstChild();node2!=null;node2=node2.getNextSibling()){
									if(node2.getNodeType()==Node.ELEMENT_NODE)
									{
										String nodevalue=node2.getFirstChild()!=null?node2.getFirstChild().getNodeValue().trim():"";
										if(node2.getNodeName().trim().equals(para.trim()))
											result=nodevalue;
									}
								}
							}
						 }
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return result;

	}
	
	
}
